#include "receivefiledialog.h"
#include "ui_receivefiledialog.h"
#include "QTimer"

FileServer fileServer;

ReceiveFileDialog::ReceiveFileDialog(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ReceiveFileDialog)
{
    ui->setupUi(this);

    ui->progressBar->setRange(0, 100);

    ui->label_message->setText("等待与" + myFriend.user_name + "连接");

    FileServer::connect(&fileServer, &FileServer::onGettingFile, [=](quint32 data1, quint32 data2){
        if (data2 > 0) {
            int progress = qRound((static_cast<double>(data1) / data2) * 100);
            ui->progressBar->setValue(progress);
            ui->label_message->setText(QString("传输中...%1%").arg(progress));
            if (progress >= 90) {
                ui->progressBar->setValue(100);
                ui->label_message->setText("传输完成");

                QTimer::singleShot(500, this, [=]() {
                    ui->progressBar->setValue(0);
                    ui->label_message->setText("等待与" + myFriend.user_name + "连接");
                });
            }
        }
    });

    fileServer.startServer(8889);
}

ReceiveFileDialog::~ReceiveFileDialog()
{
    delete ui;
}
